#pragma once
/*
 *	Copyright © 2008 University of Houston
 *	All rights reserved.
 */
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdarg.h>
#include "session.h"

using namespace std;

#define DEFAULT_LOGFILE	"logfile"

class packetLogger
{
public:
	packetLogger();
	~packetLogger();

	static const char* format_string()
	{
		return "%i.%i %u %u %s %i -> %s %i %i %i\n";
	};

	static const char* packet_identifier_string()
	{
		return "@ 0x%X\n";
	};

	/*
	 * Logs the provided session to file, with one packet per line.  Also
	 * provides a timestamp of the date and time of the logging above the
	 * actual log data.
	 * @param sessionToLog Pointer to the session object to be logged.
	 * @return No return value.
	 */
	void 	logSession(const session *sessionToLog);

	/*
	 * Provides a formatted string that contains all of the pertinent
	 * information about the provided packet.  This information is logged in
	 * the same format as is used by the logger.
	 * @param headerToFormat The header object to be formatted.
	 * @return Returns a string containing the formatted text.
	 */
	string 	formatPacketForLogging(timedHeader headerToFormat);

	/*
	 * Initializes the logger for use with the given filename.  If the file
	 * already exists, new data is appended to the end of the log.
	 * @param fileName The desired log filename.
	 * @return False if the logger could not be initialized, otherwise true.
	 */
	bool init(string fileName, string access="a+");

	/*
	 * @return Returns true if the logger has been initialized properly, and
	 * is ready for logging.
	 */
	bool isInitialized();

	/*
	 * Loads a session object from file.
	 * @param filename Name of the log file to use for loading
	 * @param ptr Pointer to the session object to be populated
	 * @param sessionNum The session number to load.
	 * @return A session object.
	 */
	bool loadSessionFromLog(string filename, session* ptr, int sessionNum);

	/*
	 * @see getLastSessionNumber(FILE* filePointer)
	 */
	int	getLastSessionNumber();
private:
	/*
	 * Returns true if the provided buffer contains actual packet data.
	 * An example of a non-data string is a log timestamp (the time at which
	 * the data was written to the log file) or a session ID number.
	 * @param buffer The buffer to check.
	 * @return True if the buffer contains packet data.  False otherwise.
	 * @see See usage in seekToSession.
	 */
	bool 	isLogData(const char* buffer) const;

	/*
	 * Returns true if the provided buffer is a Session ID log line.
	 * See packet_identifier_string for information on Session ID log
	 * formatting.
	 * @param buffer The buffer to check.
	 * @return Returns true if the line contains a Session ID.
	 */
	bool isSessionIdHeader(const char *buffer) const;


	/*
	 * Seeks the file-input read point (via fsetpos) on the given file to the
	 * specified session number.  The position will be *after* the commented
	 * line that states the session number (it will be placed where the
	 * first line of packet data should be).
	 * @param filePointer Pointer to the file handle to seek with.
	 * @param sessionID The session ID number to seek to.
	 * @return Returns true if the seek operation was successful.
	 */
	bool 	seekToSession(FILE* filePointer, int sessionID);



	/*
	 * Returns the Session ID number of the last session written to the log
	 * file.  This is useful when appending to a log file, to determine what
	 * the next assigned Session ID number should be.
	 * @param filePointer Pointer to the file object to check.
	 * @return Returns a Session ID number.
	 * @notes This function saves and restores the file seek point.  Using
	 * this function should return the file to the seek point that it was
	 * originally given.
	 */
	int		getLastSessionNumber(FILE* filePointer);

	FILE *handleToFile;					// Handle to the log file

	// Minimum amount of data that must be read from file for a read
	// operation to be successful.
	static const unsigned int MIN_READ_SIZE=14;
};
